<?php

namespace App\Admin\Services\Application;


use App\Models\Application\Application;
use App\Repositories\Application\ApplicationRepository;

class ApplicationService {
    /**
     * @return mixed
     */
    public function applicationPage() {
        return ApplicationRepository::getInstance()->queryByConditionAndPage();
    }
    
    /**
     * @param $data
     * @param $fillAble
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function applicationStore($data, $fillAble) {
        $data['app_id']     = Application::generalAppId();
        $data['secret_key'] = Application::generalSecretKey();
        $fillAble           = array_merge($fillAble, ['app_id', 'secret_key']);
        
        return ApplicationRepository::getInstance()->store($data, $fillAble);
    }
    
    /**
     * @param $id
     * @param $data
     * @param $fillAble
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     */
    public function applicationUpdate($id, $data, $fillAble) {
        return ApplicationRepository::getInstance()->update($id, $data, $fillAble);
    }
    
    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Model|mixed
     * @throws \Exception
     */
    public function applicationDestroy($id) {
        return ApplicationRepository::getInstance()->delete($id);
    }
    
    /**
     * 更新应用秘钥
     * @param $id
     *
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null
     * @throws \Exception
     */
    public function applicationUpdateSecretKey($id){
        $application             = ApplicationRepository::getInstance()->findItemElById($id);
        $application->secret_key = Application::generalSecretKey();
        $application->save();
    
        return $application;
    }
}